﻿using System;
using System.Collections.Generic;
using System.Web.Security;
using System.Web.UI.WebControls;
using visinia.SmartEngine;

namespace visinia.web.admin.pages
{
    public partial class ManageUsers : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int count = 0;
                GridViewUSERS.DataSource = Membership.Provider.GetAllUsers(0, 999, out count);
                GridViewUSERS.DataBind();
            }
        }

        protected void GridViewUSERS_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
        {
            string username = (string)GridViewUSERS.DataKeys[e.RowIndex].Value;
            string[] roles = Roles.GetRolesForUser(username);
            if(roles.Length > 0)
                Roles.RemoveUserFromRoles(username, roles);
            Membership.DeleteUser(username);
            Response.Redirect(Request.RawUrl);
        }

        protected void GridViewUSERS_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                MembershipUser user = (MembershipUser)e.Row.DataItem;
                CheckBoxList list = (CheckBoxList)e.Row.FindControl("CheckBoxListROLES");
                string[] roles = Roles.GetAllRoles();
                List<string> userRoles = new List<string>(Roles.GetRolesForUser(user.UserName));
                foreach (string role in roles)
                {
                    ListItem item = new ListItem(role);
                    if (userRoles.Contains(role))
                        item.Selected = true;
                    list.Items.Add(item);
                }
            }
        }
        protected void GridViewUSERS_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string username = ((Label)GridViewUSERS.Rows[index].Cells[0].FindControl("USERNAME")).Text;
                Response.Redirect(SystemMenu.CallPage("_Security", "EditUser", "UserName", username));
            }
        }
        protected void UPDATE_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridViewUSERS.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    string UserName = ((Label)row.Cells[0].FindControl("USERNAME")).Text;
                    List<string> userRoles = new List<string>(Roles.GetRolesForUser(UserName));
                    CheckBoxList list = (CheckBoxList)row.FindControl("CheckBoxListROLES");
                    List<string> NewRoles = new List<string>();
                    List<string> DeleteRoles = new List<string>();
                    foreach (ListItem item in list.Items)
                    {
                        if (item.Selected)
                        {
                            if (userRoles.Contains(item.Text))
                                continue;
                            else
                                NewRoles.Add(item.Text);
                        }
                        else
                        {
                            if (userRoles.Contains(item.Text))
                                DeleteRoles.Add(item.Text);
                            else
                                continue;
                        }
                    }
                    if (NewRoles.Count > 0)
                    {
                        Roles.AddUserToRoles(UserName, NewRoles.ToArray());
                    }
                    if (DeleteRoles.Count > 0)
                    {
                        Roles.RemoveUserFromRoles(UserName, DeleteRoles.ToArray());
                    }
                }
            }
        }
        protected void CANCEL_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }
        protected void NEW_USER_Click(object sender, EventArgs e)
        {
            Response.Redirect(SystemMenu.CallPage("_Security", "NewUser"));
        }
    }
}